Take-home Exercise 1b

Published

January 13, 2024

Modified

March 12, 2024

Getting Started

Loading R package

pacman::p_load(tidyverse, sf, lubridate, leaflet, tmap,
               raster, spatstat, plotly, DT, classInt, viridis,
               ggplot2, sfdep)
# Load conflict data
conflict_data <- read_csv("data/MMR.csv")

mmr_shp <-  st_read(dsn = "data/geospatial",  
                  layer = "gadm41_MMR_1")
Reading layer `gadm41_MMR_1' from data source 
  `C:\imranmi\ISSS608-VAA\Take-home-ex\Take-home-Ex1b\data\geospatial' 
  using driver `ESRI Shapefile'
Simple feature collection with 15 features and 11 fields
Geometry type: MULTIPOLYGON
Dimension:     XY
Bounding box:  xmin: 92.1725 ymin: 8.824445 xmax: 101.1768 ymax: 28.54326
Geodetic CRS:  WGS 84
# Convert conflict data to an sf object
conflict_sf <- st_as_sf(conflict_data, coords = c("longitude", "latitude"), crs = 4326)

Use the SF file, with geometry column

# Filter for battles only
conflict_sf_battles_year <- conflict_sf %>%
  filter(event_type == "Battles", year == 2010)

# Plot using leaflet
leaflet(conflict_sf_battles_year) %>%
  addTiles() %>%
  addCircleMarkers(popup = ~paste("Event: Battles, 2010<br>State/Region:", admin1, 
                                  "<br>Actor1:", actor1, "<br>Actor2:", actor2))

using the original acled file with long and lat columns

# Filter for battles only
conflict_battles_year <- conflict_data %>%
  filter(event_type == "Battles", year == 2010)


# Generate the map
leaflet() %>%
  addProviderTiles(providers$CartoDB.Positron) %>% # Base map layer
  addCircleMarkers(data = conflict_battles_year, 
                   popup = ~paste("Event: Battles, 2010<br>Admin1:", admin1, 
                                  "<br>Actor1:", actor1, "<br>Actor2:", actor2),
                   radius = 8, # Adjust the size of the markers as needed
                   fillColor = "red", fillOpacity = 0.8, color = "#FFFFFF", weight = 1) 

adding an additional layer of base map, mmr_shp to highlight visually MMR alone

leaflet() %>%
  addProviderTiles(providers$CartoDB.Positron) %>%
  addCircleMarkers(data = conflict_sf_battles_year, 
                   popup = ~paste("Event: Battles, 2010<br>Region/State:", admin1, 
                                  "<br>Actor1:", actor1, "<br>Actor2:", actor2),
                   radius = 8,
                   fillColor = "red", fillOpacity = 0.8, color = "#FFFFFF", weight = 1) %>%
  addPolygons(data = mmr_shp, color = "#444444", weight = 1, fillOpacity = 0.5) %>% # Adding borders
  setView(lng = 96.1603, lat = 19.745, zoom = 6) # Center and zoom the map on Myanmar
st_geometry(conflict_sf_battles_year) |> 
  plot()

class(conflict_sf)
[1] "sf"         "tbl_df"     "tbl"        "data.frame"
class(mmr_shp)
[1] "sf"         "data.frame"
class(conflict_data)
[1] "spec_tbl_df" "tbl_df"      "tbl"         "data.frame" 
mmr_shp[3,]
Simple feature collection with 1 feature and 11 fields
Geometry type: MULTIPOLYGON
Dimension:     XY
Bounding box:  xmin: 92.6012 ymin: 20.64611 xmax: 94.17053 ymax: 24.11297
Geodetic CRS:  WGS 84
    GID_1 GID_0 COUNTRY NAME_1  VARNAME_1 NL_NAME_1 TYPE_1 ENGTYPE_1 CC_1
3 MMR.3_1   MMR Myanmar   Chin Chin Hills        NA  Pyine     State   NA
  HASC_1 ISO_1                       geometry
3  MM.CH    NA MULTIPOLYGON (((92.66836 21...
conflict_sf
Simple feature collection with 57198 features and 33 fields
Geometry type: POINT
Dimension:     XY
Bounding box:  xmin: 92.199 ymin: 9.9824 xmax: 100.3576 ymax: 27.731
Geodetic CRS:  WGS 84
# A tibble: 57,198 × 34
   event_id_cnty event_date        year time_precision disorder_type  event_type
 * <chr>         <chr>            <dbl>          <dbl> <chr>          <chr>     
 1 MMR58558      16 February 2024  2024              1 Political vio… Battles   
 2 MMR58559      16 February 2024  2024              1 Political vio… Battles   
 3 MMR58443      16 February 2024  2024              2 Political vio… Violence …
 4 MMR58502      16 February 2024  2024              1 Political vio… Violence …
 5 MMR58507      16 February 2024  2024              1 Political vio… Explosion…
 6 MMR58508      16 February 2024  2024              1 Political vio… Explosion…
 7 MMR58547      16 February 2024  2024              1 Strategic dev… Strategic…
 8 MMR58560      16 February 2024  2024              1 Political vio… Battles   
 9 MMR58589      16 February 2024  2024              2 Political vio… Violence …
10 MMR58648      16 February 2024  2024              1 Political vio… Explosion…
# ℹ 57,188 more rows
# ℹ 28 more variables: sub_event_type <chr>, actor1 <chr>, assoc_actor_1 <chr>,
#   inter1 <dbl>, actor2 <chr>, assoc_actor_2 <chr>, inter2 <dbl>,
#   interaction <dbl>, civilian_targeting <chr>, iso <dbl>, region <chr>,
#   country <chr>, admin1 <chr>, admin2 <chr>, admin3 <chr>, location <chr>,
#   geo_precision <dbl>, source <chr>, source_scale <chr>, notes <chr>,
#   fatalities <dbl>, tags <chr>, timestamp <dbl>, population_1km <dbl>, …
plot(st_geometry(conflict_sf))

Battles <- filter(conflict_sf, event_type == "Battles")
Violence_CV <- filter(conflict_sf, event_type == "Violence against civilians")
Protests <- filter(conflict_sf, event_type == "Protests")
Riots <- filter(conflict_sf, event_type == "Riots")
Explosions <- filter(conflict_sf, event_type == "Explosions/Remote violence")
Strategic_dev <- filter(conflict_sf, event_type == "Strategic developments")
class(Explosions)
[1] "sf"         "tbl_df"     "tbl"        "data.frame"

Visualising of Fatalities by Event Type

In shiny App , we will need to enable users to filter by

  • specific year,

  • year range, and

  • event_type

Using subsets of event types which have been converted to sf objects

scaleFactor <- 2  

leaflet(Battles) %>%
  addProviderTiles(providers$CartoDB.Positron) %>%
  addCircleMarkers(popup = ~paste("Event: Battles<br>State/Region:", admin1, 
                                  "<br>Actor1:", actor1, "<br>Actor2:", actor2,
                                  "<br>Year:", year, "<br>Fatalities:", fatalities),
                   radius = ~sqrt(fatalities) * scaleFactor,
                   fillColor = "red", fillOpacity = 0.4, color = "#FFFFFF", weight = 1) %>% 
  setView(lng = 96.1603, lat = 19.745, zoom = 6)
scaleFactor <- 2  

leaflet(Violence_CV) %>%
  addProviderTiles(providers$CartoDB.Positron) %>%
  addCircleMarkers(popup = ~paste("Event: Violence on Civillians<br>State/Region:", admin1, 
                                  "<br>Actor1:", actor1, "<br>Actor2:", actor2,
                                  "<br>Year:", year, "<br>Fatalities:", fatalities),
                   radius = ~sqrt(fatalities) * scaleFactor,
                   fillColor = "red", fillOpacity = 0.4, color = "#FFFFFF", weight = 1) %>% 
  setView(lng = 96.1603, lat = 19.745, zoom = 6)
scaleFactor <- 2  

leaflet(Protests) %>%
  addProviderTiles(providers$CartoDB.Positron) %>%
  addCircleMarkers(popup = ~paste("Event: Protests<br>State/Region:", admin1, 
                                  "<br>Actor1:", actor1, "<br>Actor2:", actor2,
                                  "<br>Year:", year, "<br>Fatalities:", fatalities),
                   radius = ~sqrt(fatalities) * scaleFactor,
fillColor = "red", fillOpacity = 0.4, color = "#FFFFFF", weight = 1) %>% 
  setView(lng = 96.1603, lat = 19.745, zoom = 6)
scaleFactor <- 2  

leaflet(Riots) %>%
  addProviderTiles(providers$CartoDB.Positron) %>%
  addCircleMarkers(popup = ~paste("Event: Riots<br>State/Region:", admin1, 
                                  "<br>Actor1:", actor1, "<br>Actor2:", actor2,
                                  "<br>Year:", year, "<br>Fatalities:", fatalities),
                   radius = ~sqrt(fatalities) * scaleFactor,
                   fillColor = "red", fillOpacity = 0.4, color = "#FFFFFF", weight = 1) %>% 
  setView(lng = 96.1603, lat = 19.745, zoom = 6)
scaleFactor <- 2  

leaflet(Explosions) %>%
  addProviderTiles(providers$CartoDB.Positron) %>%
  addCircleMarkers(popup = ~paste("Event: Explosions<br>State/Region:", admin1, 
                                  "<br>Actor1:", actor1, "<br>Actor2:", actor2,
                                  "<br>Year:", year, "<br>Fatalities:", fatalities),
                   radius = ~sqrt(fatalities) * scaleFactor,
fillColor = "red", fillOpacity = 0.4, color = "#FFFFFF", weight = 1) %>% 
  setView(lng = 96.1603, lat = 19.745, zoom = 6)